//
//  XNTitleAlertView.m
//  XINNingOA
//
//  Created by samuel on 2021/7/13.
//

#import "XNTitleAlertView.h"

@interface XNTitleAlertView()

@property (nonatomic, weak)UIView *titleView;

@property (nonatomic, weak)UIView *backView;

@property (nonatomic, weak) UILabel *contentLab;

@end

@implementation XNTitleAlertView


- (instancetype)init
{
    self = [super init];
    if (self) {
        [self renderView];
    }
    return self;
}

- (void)renderView
{
    UIView *titleView = [[UIView alloc] init];
    titleView.backgroundColor = [UIColor colorWithHexString:@"#358AFF"];
    [self addSubview:titleView];
    self.titleView = titleView;
    [titleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(self);
        make.height.mas_equalTo(52);
    }];
    
    UILabel *title = [[UILabel alloc] init];
    title.text = @"提示";
    title.textColor = [UIColor whiteColor];
    title.textAlignment = NSTextAlignmentCenter;
    title.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
    [titleView addSubview:title];
    [title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(titleView);
        make.height.mas_equalTo(20);
    }];
    
    
    UILabel *cancel = [[UILabel alloc] init];
    cancel.text = @"确定";
    cancel.backgroundColor = [UIColor whiteColor];
    cancel.userInteractionEnabled = YES;
    cancel.textColor = [UIColor blackColor];
    cancel.textAlignment = NSTextAlignmentCenter;
    cancel.font = [UIFont systemFontOfSize:18 weight:UIFontWeightMedium];
    [self addSubview:cancel];
    [cancel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self);
        make.height.mas_equalTo(40);
    }];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissPopView:)];
    [cancel addGestureRecognizer:tap];
    
    
    UIView *contentView = [[UIView alloc] init];
    contentView.backgroundColor = [UIColor whiteColor];
    [self addSubview:contentView];
    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(titleView.mas_bottom);
        make.left.right.equalTo(self);
        make.bottom.equalTo(cancel.mas_top);
    }];
    
    UILabel *contentLab = [[UILabel alloc] init];
    contentLab.font = [UIFont systemFontOfSize:14];
    contentLab.numberOfLines = 0;
    [contentView addSubview:contentLab];
    self.contentLab = contentLab;
    [contentLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(contentView).offset(10);
        make.left.equalTo(contentView).offset(10);
        make.bottom.right.equalTo(contentView).offset(-10);
    }];
    
    
    UILabel *line = [[UILabel alloc] init];
    line.backgroundColor = [UIColor colorWithHexString:@"#E0E0E0"];
    [self addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self);
        make.height.mas_equalTo(1);
        make.bottom.equalTo(cancel.mas_top);
    }];
    
}

- (void)setAlertContent:(NSString *)alertContent
{
    _alertContent = alertContent;
    self.contentLab.text = alertContent;
}

- (void)dismissPopView:(UITapGestureRecognizer *)tap
{
    [self dismmisPop];
}


//出现
- (void)show {
    UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
    UIView *backView = [[UIView alloc] initWithFrame:keyWindow.bounds];
    self.backView = backView;
    self.backView.alpha = 0.3;
    self.backView.backgroundColor = [UIColor blackColor];
    [keyWindow addSubview:self.backView];
    [keyWindow addSubview:self];
    [keyWindow bringSubviewToFront:self];
    
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(30);
        make.right.mas_equalTo(-30);
        make.centerY.equalTo(backView);
    }];
    
    [keyWindow layoutIfNeeded];

}

//消失
- (void)dismmisPop {
    [self endEditing:YES];
    UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
    [keyWindow layoutIfNeeded];
    [self.backView removeFromSuperview];
    [self removeFromSuperview];

}


@end
